package bTree.LeetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class 多数元素2 {
    public List<Integer> majorityElement(int[] nums) {
        HashMap<Integer,Integer>hashMap=new HashMap<>();
        for (int num : nums) {
            if (!hashMap.containsKey(num)){
                hashMap.put(num, 1);
            }else {
                hashMap.put(num, hashMap.get(num)+1);
            }
        }
        List<Integer> collect = hashMap.entrySet().stream().filter(a -> a.getValue() > nums.length / 3).map(Map.Entry::getKey).collect(Collectors.toList());
        return collect;
    }
}